package com.xxx.car.web.admin;

import com.github.pagehelper.Page;
import com.xxx.car.entity.vo.request.ReqFeeSetting;
import com.xxx.car.entity.vo.request.ReqFeeSettingDel;
import com.xxx.car.entity.vo.request.ReqFeeSettingQueryForAdmin;
import com.xxx.car.entity.vo.response.RespFeeSetting;
import com.xxx.car.service.FeeSettingService;
import com.xxx.common.base.enums.AdminErrorEnum;
import com.xxx.common.base.vo.AdminResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by wenjiexu on 2017/7/7.
 */
@Controller
@RequestMapping("/admin/feeSetting")
public class AdminFeeSettingController {

    @Autowired
    private FeeSettingService feeSettingService;

    @RequestMapping("/")
    public String index() {
        return "feeSetting";
    }

    @RequestMapping("/list")
    @ResponseBody
    public AdminResponse list(@Valid @ModelAttribute ReqFeeSettingQueryForAdmin reqFeeSettingQueryForAdmin, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        Page<RespFeeSetting> page = feeSettingService.query(reqFeeSettingQueryForAdmin);
        return AdminResponse.returnSuccess(page.getPageNum(), page.getPageSize(), page.getPages(), page.getTotal(), page.getResult());
    }

    @RequestMapping("/del")
    @ResponseBody
    public AdminResponse del(@Valid @ModelAttribute ReqFeeSettingDel reqFeeSettingDel, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        feeSettingService.delteById(reqFeeSettingDel.getId());
        return AdminResponse.returnSuccess();
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AdminResponse saveOrUpdate(@Valid @ModelAttribute ReqFeeSetting reqFeeSetting, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        feeSettingService.saveOrUpdate(reqFeeSetting);
        return AdminResponse.returnSuccess();
    }

}
